<?php
namespace app\task\controller;
use app\task\model\Order as OrderModel;

/**
 * 第三方配送异步通讯接口
 */

class Delivery
{
    /**
     * 达达异步通知处理
     */
    public function dada()
    {
        $msg = file_get_contents ('php://input' );
		parse_str($msg,$data); //get字符串转换成数组$data
		write_log('达达异步通知',__DIR__);
		write_log($msg,__DIR__);
		write_log($data,__DIR__);
		
    }
	/**
     * 达达异步通知处理
     */
    public function shunfeng()
    {
		/*
		回调URL（配送状态更改） https://food.cityphp.cn/shunfeng.php
		回调URL（订单完成） https://food.cityphp.cn/shunfeng.php
		回调URL（顺丰原因取消） https://food.cityphp.cn/shunfeng.php
		回调URL（订单配送异常） https://food.cityphp.cn/shunfeng.php
		*/
        $msg = file_get_contents ('php://input' );
		$data = json_decode($msg,true);
		write_log('顺丰 - 异步通知',__DIR__);
		write_log($data,__DIR__);
		if(isset($data['sf_order_id'])){
			$order = OrderModel::getDetail($data['sf_order_id']);
			//订单状态回调
			if($data['url_index']=='rider_status'){
				//配送员已结单
				if($data['order_status']==10){
					//配送员已指派
					if($order['delivery_status']['value']==20 AND $order['receipt_status']['value']==10){
						$order->receipt_status = 20;
						$order->deliver_name = $data['operator_name'];//骑手姓名
						$order->deliver_mobile = $data['operator_phone'];//骑手电话
						//$order->deliver_location = $data['rider_lat'].$data['rider_lng'];//骑手位置
						$order->save();
					}
				}
				//配送员到店
				if($data['order_status']==12){
					//配送员已到店
				}
				//配送员配送中
				if($data['order_status']==15){
					//配送员已取货
				}
			}
			//订单完成回调
			if($data['url_index']=='order_complete'){
				//配送已完成
				if($data['order_status']==17){
					//配送员完成
					if($order['receipt_status']['value']==20){
						$order->receipt_status = 30;
						$order->save();
					}
				}
			}
			//订单取消回调
			if($data['url_index']=='sf_cancel'){
				//顺丰原因取消
				if($data['order_status']==2){
					//顺丰取消订单
					$order->delivery_status = 10;
					$order->receipt_status = 10;
					$order->deliver_mode = 0;
					$order->save();
				}
			}
		}
		return true;
    }
	
	/**
	 * 错误代码
	 */
	private function p_err($err_code)
	{
		
	}
	
}
